package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApCollectionMapper;
import com.heima.article.service.ApCollectionService;
import com.heima.feigns.behavior.BehaviorFeign;
import com.heima.model.behavior.dtos.CollectionBehaviorDto;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApCollection;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApCollectionServiceImpl extends ServiceImpl<ApCollectionMapper, ApCollection> implements ApCollectionService {

    @Autowired
    BehaviorFeign behaviorFeign;

    /**
     * 保存收藏
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult collect(CollectionBehaviorDto dto) {
        //参数校验
        if (dto == null || dto.getEntryId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //查询行为实体

        ApUser user = AppThreadLocalUtils.getUser();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        ApBehaviorEntry behaviorEntry = behaviorFeign.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());
        if (behaviorEntry == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        ApCollection apCollection = getOne(Wrappers.<ApCollection>lambdaQuery()
                .eq(ApCollection::getEntryId, behaviorEntry.getId())
                .eq(ApCollection::getArticleId, dto.getEntryId())
        );

        if (apCollection == null && dto.getOperation().equals(ApCollection.Type.DYNAMIC.getCode())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE, "暂无收藏");
        }

        if (apCollection != null
                && dto.getOperation().equals(ApCollection.Type.ARTICLE.getCode())
                && ApCollection.Type.ARTICLE.getCode() == dto.getType().shortValue()
                && dto.getType().shortValue() == apCollection.getType().shortValue()
        ) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "重复收藏");
        }

        //保存收藏
        if (apCollection == null && dto.getType().equals(ApCollection.Type.ARTICLE.getCode())) {
            apCollection = new ApCollection();
            apCollection.setArticleId(dto.getEntryId());
            apCollection.setCollectionTime(new Date());
            apCollection.setEntryId(behaviorEntry.getId());
            apCollection.setType(dto.getType());
            apCollection.setPublishedTime(dto.getPublishedTime());


            save(apCollection);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }else {//取消保存
            apCollection.setType(dto.getOperation());


            updateById(apCollection);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
    }
}
